package com.aaa.controller.sas;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.Result;
import com.aaa.entity.ReturnRecords;
import com.aaa.entity.sas.TestResultSas;
import com.aaa.entity.system.PageOrder;
import com.aaa.service.sas.TestResultSasService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;

/**
 * (TestResultSas)表控制层
 *
 * @author makejava
 * @since 2022-09-02 20:05:15
 */
@RestController
@RequestMapping("testResultSas")
public class TestResultSasController extends BaseController {

    /**
     * 服务对象
     */
    @Autowired
    private TestResultSasService testResultSasService;


    /**
     *
     * @description:查询所有
     * @create time:

     * @return
     */

    @RequestMapping("queryAll.do")
    public String queryAll(PageOrder pageOrder, TestResultSas testResultSas){
        Page page = testResultSasService.queryAllTestResult(pageOrder, testResultSas);
        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(page.getTotal(), page.getResult()), jsonConfig);
        return jsonStr;
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("deleteResultSasById.do")
    @ResponseBody
    public Result deleteResultSasById(int[] id){
        int i = testResultSasService.deleteResultSasById(id);
        if (i>0){
            return success();
        }else {
            return fail();
        }

    }
    //用户查询页面
    @RequestMapping("toShowSasResult.do")
    public ModelAndView toShowUser(ModelAndView modelAndView){
        modelAndView.setViewName("sas/showResultSas");
        return modelAndView;
    }
    /**
     * 结果显示页面
     */
    @RequestMapping("queryBySasTester.do")
    public Result queryBySasTester(int id,String type){
        List<TestResultSas> list = testResultSasService.queryBySasTester(id,type);
        if (ObjectUtil.isNotEmpty(list)){
            for(TestResultSas testResultSas:list){
                return success(testResultSas);
            }
        }
        return fail();
    }
    @RequestMapping("getPlanSASId.do")
    public Result getPlanSASId() {
        List<Map<String, Object>> planId = testResultSasService.getPlanSASId();
        if (ObjectUtil.isNotEmpty(planId)) {
            return success(planId);
        }
        return fail();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @RequestMapping("queryResult.do")
    public  Result queryResultById(Integer  id) {
        List<TestResultSas> testResultSas = testResultSasService.queryResultById(id);
        for (TestResultSas testResultSas1 : testResultSas) {

            return success(testResultSas1);

        }
        return fail();
    }


    @RequestMapping("queryQuestionSAS.do")
    public Result getQuestionMHT(PageOrder pageOrder , Integer id) {
        Page<Map<String, Object>> byTesterId = testResultSasService.getByTesterId(pageOrder, id);
        if (ObjectUtil.isNotEmpty(byTesterId)) {
            return success(byTesterId.getTotal(), byTesterId.getResult());
        }
        return fail();
    }

    @RequestMapping("queryReturn.do")
    public Object queryReturn(PageOrder pageOrder , ReturnRecords records) {
        Page<ReturnRecords> byTesterId = testResultSasService.getReturnRecords(pageOrder, records);
        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(byTesterId.getTotal(), byTesterId.getResult()), jsonConfig);
        return jsonStr;
    }

    @RequestMapping("insertReturn.do")
    public Result insertReturn( ReturnRecords records) {
        int i = testResultSasService.insertReturn(records);
        if (i>0) {
            return success( );
        }
        return fail();
    }

    /**
     * 通过id删除回访记录的一条或多条数据
     * @param id
     * @return
     */
    @RequestMapping("deleteReturn.do")
    public Result delete( int[] id) {
        int i = testResultSasService.deleteReturn(id);
        if (i>0) {
            return success( );
        }
        return fail();
    }
    /**
     * 通过id修改回访记录
     * @param returnRecords
     * @return
     */
    @RequestMapping("updateReturn.do")
    public Result updateReturn(ReturnRecords returnRecords ) {
        int i = testResultSasService.updateReturn(returnRecords);
        if (i>0) {
            return success( );
        }
        return fail();
    }
    //用户查询页面
    @RequestMapping("toShowSASReturn.do")
    public ModelAndView toShowMHTReturn(ModelAndView modelAndView){
        modelAndView.setViewName("sas/showSASReturn");
        return modelAndView;
    }

}

